package com.besttone.kafka.consumer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
import org.springframework.kafka.listener.MessageListener;
import org.springframework.kafka.listener.config.ContainerProperties;

@Configuration
@EnableKafka
public class KafkaConfig {
	/* --------------consumer configuration----------------- **/
	@Bean
	public Map<String, Object> consumerConfigs() {
		Map<String, Object> props = new HashMap<>();
		props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092,127.0.0.1:9093,127.0.0.1:9094");
		props.put(ConsumerConfig.GROUP_ID_CONFIG, "0");// 同一个组内的消费者是实现负载均衡的
		props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
		List<String> list=new ArrayList<String>();
		list.add("org.apache.kafka.clients.consumer.RangeAssignor");
		list.add("org.apache.kafka.clients.consumer.RoundRobinAssignor");
		props.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG , list);
		props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 100);
		props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");
		props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
		props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
		return props;
	}

	@Bean
	ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
		ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
		factory.setConsumerFactory(consumerFactory());
		return factory;
	}

	@Bean
	public ConsumerFactory<String, String> consumerFactory() {
		return new DefaultKafkaConsumerFactory<>(consumerConfigs());
	}

	
	
	// 消费者容器配置信息
	@Bean
	public ContainerProperties containerProperties(MessageListener<String, String> myMessageListener) {
		Pattern topicPattern = Pattern.compile("test"); // 匹配满足正则的topic
		ContainerProperties containerProperties = new ContainerProperties(topicPattern);// 订阅满足正则表达式的topic
		containerProperties.setMessageListener(myMessageListener);// 订阅的topic的消息用myMessageListener去处理
		return containerProperties;
	}

	@Bean
	public KafkaMessageListenerContainer<String, String> kafkaMessageListenerContainer(
			ConsumerFactory<String, String> consumerFactory, ContainerProperties containerProperties) {
		return new KafkaMessageListenerContainer<>(consumerFactory, containerProperties);
	}
	
	@Bean
	public ConcurrentKafkaListenerContainerFactory<String, String> concurrentMessageListenerContainer(
			ConsumerFactory<String, String> consumerFactory, ContainerProperties containerProperties) {
		ConcurrentKafkaListenerContainerFactory<String, String> concurrentKafkaListenerContainerFactory=new ConcurrentKafkaListenerContainerFactory<String, String>();
		concurrentKafkaListenerContainerFactory.setConsumerFactory(consumerFactory);
		return concurrentKafkaListenerContainerFactory;
	}

}
